package flc.ast.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import flc.ast.bean.MyFileBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class FileAdapter extends StkProviderMultiAdapter<MyFileBean> {

    public FileAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(88));
        addItemProvider(new FileProvider());
    }

    private class FileProvider extends BaseItemProvider<MyFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_file;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyFileBean bean) {
            baseViewHolder.setImageResource(R.id.ivFileItemLog, getImgIcon(bean.getPath()));
            baseViewHolder.setText(R.id.tvFileItemName, bean.getName());
            if (FileUtils.isFile(bean.getPath())) {
                baseViewHolder.getView(R.id.tvFileItemSize).setVisibility(View.VISIBLE);
                baseViewHolder.setText(R.id.tvFileItemSize, ConvertUtils.byte2FitMemorySize(bean.getSize(), 1));
            } else {
                baseViewHolder.getView(R.id.tvFileItemSize).setVisibility(View.GONE);
            }
            long modified = FileUtils.getFileLastModified(bean.getPath());
            baseViewHolder.setText(R.id.tvFileItemDate, TimeUtils.millis2String(modified, "yyyy-M-d"));
        }
    }

    //根据后缀获取图标
    private int getImgIcon(String path) {
        String suffix = FileUtils.getFileExtension(path);
        int icon;
        if (suffix.equalsIgnoreCase("pdf")) {
            icon = R.drawable.apdf;
        } else if (suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx")) {
            icon = R.drawable.appt;
        } else if (suffix.equalsIgnoreCase("txt")) {
            icon = R.drawable.atxt;
        } else if (suffix.equalsIgnoreCase("doc") || suffix.equalsIgnoreCase("docx")) {
            icon = R.drawable.adoc;
        } else if (suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) {
            icon = R.drawable.axls;
        } else if (suffix.equalsIgnoreCase("rar") || suffix.equalsIgnoreCase("zip")) {
            icon = R.drawable.ayasuobao;
        } else {
            icon = R.drawable.awenjianbao;
        }
        return icon;
    }
}
